home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
newmat.exe
/
NEWMAT.HXX
< prev
next >
Wrap
Text File
|
1991-07-30
|
24KB
|
674 lines
//$$ newmat.hxx definition file for new version of matrix package
// Copyright (C) 1991: R B Davies and DSIR
#ifndef MATRIX_LIB
#define MATRIX_LIB 0
#ifdef NO_LONG_NAMES
#define UpperTriangularMatrix UTMatrix
#define LowerTriangularMatrix LTMatrix
#define SymmetricMatrix SMatrix
#define DiagonalMatrix DMatrix
#endif
#include "boolean.hxx"
/**************************** general utilities ****************************/
void MatrixError(char*); // error handler
void MatrixErrorNoSpace(void*); // no space handler
class LogAndSign
// Return from LogDeterminant function
// - value of the log plus the sign (+, - or 0)
{
real log_value;
int sign;
public:
LogAndSign() { log_value=0.0; sign=1; }
void operator*=(real);
void ChangeSign() { sign = -sign; }
real LogValue() { return log_value; }
int Sign() { return sign; }
real Value();
};
// the following class is for counting the number of times a piece of code
// is executed. It is used for locating any code not executed by test
// routines. Use turbo GREP locate all places this code is called and
// check which ones are not accessed.
// Somewhat implementation dependent as it relies on "cout" still being
// present when ExeCounter objects are destructed.
class ExeCounter
{
int line; // code line number
int fileid; // file identifier
long nexe; // number of executions
static int nreports; // number of reports
public:
ExeCounter(int,int);
void operator++() { nexe++; }
~ExeCounter(); // prints out reports
};
/**************************** class MatrixType *****************************/
// Is used for finding the type of a matrix resulting from the binary operations
// +, -, * and identifying what conversions are permissible.
// This class must be updated when new matrix types are added.
class GeneralMatrix; // defined later
class MatrixType
{
public:
enum Type { UnSp,UT,LT,Rect,Sym,Diag,RowV,ColV,EqEl,Crout };
static nTypes() { return 8; } // number of different types
// exclude Crout, UnSp
private:
Type type;
public:
MatrixType operator+(const MatrixType&) const;
MatrixType operator-(const MatrixType&) const;
MatrixType operator*(const MatrixType&) const;
BOOL operator>=(const MatrixType&) const;
BOOL operator==(const MatrixType& t) const; // { return (type == t.type); }
BOOL operator!=(const MatrixType& t) const; // { return !(*this == t); }
BOOL operator!() const { return type == UnSp; }
MatrixType operator-() const; // type of negative
MatrixType i() const; // type of inverse
MatrixType t() const; // type of transpose
MatrixType sub() const; // type of submatrix
MatrixType ssub() const; // type of sym submatrix
MatrixType (Type tx) : type(tx) {} // (& doesn't work with AT&T)
MatrixType () {}
GeneralMatrix* New() const; // new matrix of given type
GeneralMatrix* New(int,int) const; // new matrix of given type
operator int() const { return (int)type; }
operator char*() const; // for printing type
};
void TestTypeAdd(); // test +
void TestTypeMult(); // test *
void TestTypeOrder(); // test >=
/*************************** Matrix routines ***************************/
class MatrixRowCol; // defined later
class MatrixRow;
class MatrixCol;
class GeneralMatrix; // defined later
class AddedMatrix;
class MultipliedMatrix;
class SubtractedMatrix;
class SolvedMatrix;
class ShiftedMatrix;
class ScaledMatrix;
class TransposedMatrix;
class NegatedMatrix;
class InvertedMatrix;
class RowedMatrix;
class ColedMatrix;
class DiagedMatrix;
class MatedMatrix;
class GetSubMatrix;
class ConstMatrix;
class Matrix;
class RowVector;
class ColumnVector;
class SymmetricMatrix;
class UpperTriangularMatrix;
class LowerTriangularMatrix;
class DiagonalMatrix;
class CroutMatrix;
static MatrixType MatrixTypeUnSp(MatrixType::UnSp);
// AT&T needs this
class BaseMatrix // base of all matrix classes
{
protected:
// BaseMatrix() {}
virtual GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp) = 0;
// evaluate temporary
virtual int search(const GeneralMatrix*) const = 0;
// count number of times matrix
// is referred to
virtual MatrixType Type() const = 0; // type of a matrix
virtual int NrowsV() const = 0;
virtual int NcolsV() const = 0;
public:
void MatrixParameters(int& nr, int& nc, MatrixType& mt)
{ nr = NrowsV(); nc = NcolsV(); mt = Type(); }
AddedMatrix operator+(BaseMatrix&); // results of operations
MultipliedMatrix operator*(BaseMatrix&);
SubtractedMatrix operator-(BaseMatrix&);
ShiftedMatrix operator+(real);
ScaledMatrix operator*(real);
ScaledMatrix operator/(real);
ShiftedMatrix operator-(real);
TransposedMatrix t();
NegatedMatrix operator-(); // change sign of elements
InvertedMatrix i();
RowedMatrix CopyToRow();
ColedMatrix CopyToColumn();
DiagedMatrix CopyToDiagonal();
MatedMatrix CopyToMatrix(int,int);
GetSubMatrix SubMatrix(int,int,int,int);
GetSubMatrix SymSubMatrix(int,int);
GetSubMatrix Row(int);
GetSubMatrix Rows(int,int);
GetSubMatrix Col(int);
GetSubMatrix Cols(int,int);
virtual LogAndSign LogDeterminant();
virtual real SumSquare();
virtual real Trace();
// virtual ~BaseMatrix() {}
friend GeneralMatrix;
friend Matrix;
friend RowVector;
friend ColumnVector;
friend SymmetricMatrix;
friend UpperTriangularMatrix;
friend LowerTriangularMatrix;
friend DiagonalMatrix;
friend CroutMatrix;
friend AddedMatrix;
friend MultipliedMatrix;
friend SubtractedMatrix;
friend SolvedMatrix;
friend ShiftedMatrix;
friend ScaledMatrix;
friend TransposedMatrix;
friend NegatedMatrix;
friend InvertedMatrix;
friend RowedMatrix;
friend ColedMatrix;
friend DiagedMatrix;
friend MatedMatrix;
friend GetSubMatrix;
friend ConstMatrix;
};
/******************************* working classes **************************/
class GeneralMatrix : public BaseMatrix // declarable matrix types
{
protected:
int tag; // shows whether can reuse
int nrows, ncols; // dimensions
int storage; // total store required
real* store; // point to store (0=not set)
GeneralMatrix(); // initialise with no store
GeneralMatrix(int); // constructor getting store
GeneralMatrix* Evaluate(MatrixType);
void Add(GeneralMatrix*, real); // sum of GM and real
void Add(real); // add real to this
void Multiply(GeneralMatrix*, real); // product of GM and real
void Multiply(real); // multiply this by real
void Negate(GeneralMatrix*); // change sign
void Negate(); // change sign
void operator=(real); // set matrix to constant
real* GetStore(); // get store or copy
GeneralMatrix* BorrowStore(GeneralMatrix*, MatrixType);
// temporarily access store
void GetMatrix(GeneralMatrix*); // used by = and initialise
#ifndef __ZT